package view;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import model.Paper;
import model.Review;
import model.User;

/**
 * Where the user is able to see all of the reviews when Reviews are submitted.
 * 
 * @author Janelle
 *
 */
public class ViewReviewPanel extends JScrollPane {
	private static String REGEX = "[.][^.]+$"; // replaces the extension of a file. Found at StackOverflow question 924394
	private JPanel myPanel;
	private Paper paper;
	private User user; 

	/**
	 * the constructor of the viewReviewPanel. Initializes all of the visuals for the panel.
	 * 
	 * @param u the User
	 * @param p the Paper
	 */
	public ViewReviewPanel(User u, Paper p) {
		super();
		myPanel = new JPanel();
		paper = p;
		user = u;

		String fileNameNoExt = paper.getPaper().getName().replaceFirst(REGEX, "");
		File path = new File("papers\\" + String.valueOf((user.getConference().getConferenceId())) + "\\" + paper.getAuthor() + "\\" + fileNameNoExt + "\\reviews\\");
		File[] reviews1 = path.listFiles(); // gets all reviews
		JTextPane text = new JTextPane(); // holds everything
		text.setEditable(false);
		StringBuilder str = new StringBuilder(); //string of everything
		//goes through the reviews
		for(File r : reviews1) {
			try {
				Scanner scanner = new Scanner(r);
				while(scanner.hasNext()) {
					str.append(scanner.nextLine() + "\n");
				}
				scanner.close();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			str.append("=====END OF REVIEW=====\n");
		}
		// goes through recommendation
		File rec = new File("papers\\" + String.valueOf((user.getConference().getConferenceId())) + "\\" + paper.getAuthor() + "\\" + fileNameNoExt +  "\\recommendation\\recommendation.txt");
		if(rec != null) {
			try {
				Scanner scanner = new Scanner(rec);
				while(scanner.hasNext()) {
					str.append(scanner.nextLine() + "\n");
				}
				scanner.close();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			str.append("=====END OF RECOMMENDATION=====\n");
		}
		//goes through acceptance
		File AR = new File("papers\\" + String.valueOf((user.getConference().getConferenceId())) + "\\" + paper.getAuthor() + "\\" + fileNameNoExt + "\\acceptance.txt");
		if(AR != null) {
			try {
				Scanner scanner = new Scanner(AR);
				while(scanner.hasNext()) {
					str.append(scanner.nextLine() + "\n");
				}
				scanner.close();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			str.append("=====END OF ACCEPTANCE STATUS=====\n");
		}
		text.setText(str.toString());
		myPanel.add(text);
		this.setViewportView(myPanel);
	}
}
